import {GLMVersion, GptVersion} from "@/app/constants";
import {MessageRole} from "@/types/chat";
import { useAccessStore } from "@/app/store/access";
import {Record} from "@/types/role";
// const host_url : string = "http://localhost:8090";
const host_url : string = "http://localhost:8090";
const lottery_api_host_url : string = "http://localhost:8081";
// 定义接口
export const completions = (data : {
    messages : {content:string ,  role: MessageRole }[],
    model: any,
    huiHuaFlag : number
}) =>{
    const controller = new AbortController();
    const signal = controller.signal;
    // 设置超时时间（例如，这里设置为 15 秒）
    const timeout = setTimeout(() => {
        console.log('定时器。。。。。。。。。');
        // 如果超过 15 秒仍未收到服务器响应，中断连接并进行错误处理
        controller.abort();
        console.error('连接超时，请检查您的网络连接或稍后重试。');
    }, 50000); // 15秒

    return fetch(`${host_url}/api/v1/openai/chat/completions`,{
        method: "POST",
        headers: getHeaders(),
        body: JSON.stringify(data),
        signal:signal
    }).then((res) => {
        // 请求成功，执行相应的操作
        clearTimeout(timeout); // 清除超时设置
        return res; // 处理响应数据
    })

}
/**
 * Header 信息
 */
function getHeaders() {
    const accessState = useAccessStore.getState()

    const headers =  {
        Authorization:  accessState.token,
        'Content-Type': 'application/json;charset=utf-8'
    }

    return headers
}

// 登录
export const login = (code:string) =>{
    console.log("验证码 ："+code)
    const accessState = useAccessStore.getState()
    return fetch(`${host_url}/api/v1/auth/login`,{
        method: "POST",
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        body: `code=${accessState.accessCode}`
    })
}
// 查询商品列表
export const queryProductList = () =>{
    return fetch(`${host_url}/api/v1/sale/query_product_list`,{
        method: "GET",
        headers: getHeaders()
    })
}
// 用户下单

export const createOrder = (productId: number,payType: number) => {
   const data = {"productId":productId,
       "payType":payType}

    return fetch(`${host_url}/api/v1/sale/mock_pay`,{
        method: "POST",
        headers: getHeaders(),
        body: JSON.stringify(data),
    })
}


// 添加记录
export const addRecord = (data:any) => {
    return fetch(`${host_url}/api/v1/record/add`, {
        method: "POST",
        headers: getHeaders(),
        body: JSON.stringify(data),
    })
}
// 查询记录列表
export const queryRecordList = () =>{
    return fetch(`${host_url}/api/v1/record/queryRecordList`,{
        method: "GET",
        headers: getHeaders()
    })
}


// 直接调用有跨域问题？
// 需要更改
//  抽奖相关
export const queryAwareList = (activityId:any) =>{
    return fetch(`${lottery_api_host_url}/api/queryStrategyExportList?activityId=${activityId}`,{
        method: "GET",
        headers: getHeaders()
    })
}
// 执行抽奖
export const doDrawExecByUId = (activityId:any,uid:any) =>{
    return fetch(`${lottery_api_host_url}/api/doDrawExec?activityId=${activityId}&uid=${uid}`,{
        method: "GET",
        headers: getHeaders()
    })
}


// 新接口
// 查询用户信息，入参就是token
export const queryUserInfo = () => {

}















